package luj.tool.jnproxyan.proxy.stats.http.history;

import luj.tool.jnproxyan.proxy.stats.http.history.state.HttpHistoryDat;
import luj.tool.jnproxyan.proxy.stats.http.history.state.HttpHistoryItem;
import org.springframework.stereotype.Component;

import java.util.Comparator;

@Component
public class HistoryCurItemGetter {

  public HistoryCurItemGetter inject(HttpHistoryDat historyDat, Long socksId) {
    var result = new HistoryCurItemGetter();
    result._historyDat = historyDat;
    result._socksId = socksId;
    return result;
  }

  public HttpHistoryItem getCurrent() {
    return _historyDat.getItemList().stream()
        .filter(i -> i.getSocksId().equals(_socksId))
        .max(Comparator.comparing(HttpHistoryItem::getItemId))
        .orElseThrow(() -> new IllegalArgumentException("找不到请求，socks：" + _socksId));
  }

  HttpHistoryDat _historyDat;

  Long _socksId;
}
